package com.jbp.controller;

import com.jbp.model.Article;
import com.jbp.model.ArticleLike;
import com.jbp.service.ArticleService;
import com.jbp.service.KeywordService;
import com.jbp.utils.PageUtils;
import com.jbp.utils.R;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Map;

/**
 * 动态(Article)表控制层
 * @author cc
 * @since 2020-08-25 14:37:34
 */
@Api(tags ="APP接口--动态" )
@RestController
@ApiIgnore
@RequestMapping("rest/article")
public class ArticleController {
    @Autowired
    private ArticleService articleService;
    @Autowired
    private KeywordService keywordService;
    /** 列表 */
    @ApiOperation(value="列表")
    @ApiResponses({
            @ApiResponse(code = 200,message = "成功",response = Article.class),
            @ApiResponse(code = 201,message = "返回失败信息")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "authorId",value = "作者id",paramType = "body"),
            @ApiImplicitParam(name = "type",value = "类型 push推荐，new最新，focus 关注",paramType = "body"),
    })
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params, @RequestAttribute("userId") Integer userId){
        params.put("userId", userId);
        PageUtils page = articleService.queryPage(params);
        return R.ok().put("page", page);
    }
    
//    @ApiOperation(value="信息")
//    @GetMapping("/info/{id}")
//    public R info(@PathVariable("id") Integer id){
//        return R.ok(articleService.getById(id) );
//    }
    
    /** 保存 */
    @ApiOperation(value="保存")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type",value = "类型 word 文本， image 图片， video 视频， voice 声音",paramType = "body"),
            @ApiImplicitParam(name = "content",value = "文本",paramType = "body"),
            @ApiImplicitParam(name = "files",value = "文件列表，多个文件用,分隔",paramType = "body"),
    })
    @PostMapping("/save")
    public R save(@RequestBody Article article, @RequestAttribute("userId") Integer userId){
        keywordService.checkContent(article.getContent());
        article.setAuthorId(userId);
        articleService.add(article);
        return R.ok(article);
    }

    /** 修改 */
    @ApiOperation(value="修改")
    @PostMapping("/update")
    public R update(@RequestBody Article article, @RequestAttribute("userId") Integer userId){
        if (!userId.equals(article.getAuthorId())) {
            return R.error("只有作者才能修改");
        }
        keywordService.checkContent(article.getContent());
        articleService.updateById(article);
        return R.ok();
    }

    /** 删除 */
    @ApiOperation(value="删除")
    @PostMapping("/delete")
    public R delete(@RequestBody Article req, @RequestAttribute("userId") Integer userId){
        Article article = articleService.getById(req.getId());
        if (!userId.equals(article.getAuthorId())) {
            return R.error("只有作者才能删除");
        }
        articleService.remove(req.getId());
        return R.ok();
    }
}